import { createRouter, createWebHistory, RouteRecordRaw, Router } from "vue-router";
import HelloWorld from "../components/HelloWorld.vue";

const routes: Array<RouteRecordRaw> = [
    {
        path: "/",
        name: "HelloWorld",
        component: HelloWorld,
    },
    {
        path: "/about",
        name: "About",
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import(/* webpackChunkName: "About" */ "../components/About.vue")
    },
    {
        path: "/label",
        name: "Label",
        component: () =>
            import(/* webpackChunkName: "Label" */ "../views/label/index.vue")
    }

];

const router = createRouter({
    history: createWebHistory(),
    //  history: VueRouter.createWebHashHistory(),
    routes,
});
/** 路由守卫  */
export function createPermissionGuard(router: Router) {
    router.beforeEach(async (to, from, next) => {
        if (to.path.match(/^\/user/) != null) {
            // store.commit('user/setUserType', 0)
        }
        const redirectPath = (from.query.redirect || to.path) as string;
        const redirect = decodeURIComponent(redirectPath);
        const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect };
        next(nextData);
    });
}

// createPermissionGuard(router)

export default router;